﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
    [Serializable()]
    public class RThuChiBO : Csla.BusinessBase<RThuChiBO>
    {
        #region Business Properties

        private int? _ThuChiID = null;
        public int? ThuChiID
        {
            get { return this._ThuChiID; }
            set { this._ThuChiID = value; }
        }

        private int? _LoaiPhieu = null;
        public int? LoaiPhieu
        {
            get { return this._LoaiPhieu; }
            set { this._LoaiPhieu = value; }
        }

        private string _BoPhan = null;
        public string BoPhan
        {
            get { return this._BoPhan; }
            set { this._BoPhan = value; }
        }

        private DateTime? _NgayLapPhieu = null;
        public DateTime? NgayLapPhieu
        {
            get { return this._NgayLapPhieu; }
            set { this._NgayLapPhieu = value; }
        }

        private DateTime? _NgayKyNhan = null;
        public DateTime? NgayKyNhan
        {
            get { return this._NgayKyNhan; }
            set { this._NgayKyNhan = value; }
        }

        private string _QuyenSo = null;
        public string QuyenSo
        {
            get { return this._QuyenSo; }
            set { this._QuyenSo = value; }
        }

        private string _So = null;
        public string So
        {
            get { return this._So; }
            set { this._So = value; }
        }

        private string _No = null;
        public string No
        {
            get { return this._No; }
            set { this._No = value; }
        }

        private string _Co = null;
        public string Co
        {
            get { return this._Co; }
            set { this._Co = value; }
        }

        private string _HoTen = null;
        public string HoTen
        {
            get { return this._HoTen; }
            set { this._HoTen = value; }
        }

        private string _DiaChi = null;
        public string DiaChi
        {
            get { return this._DiaChi; }
            set { this._DiaChi = value; }
        }

        private string _LyDo = null;
        public string LyDo
        {
            get { return this._LyDo; }
            set { this._LyDo = value; }
        }

        private double? _SoTien = null;
        public double? SoTien
        {
            get { return this._SoTien; }
            set { this._SoTien = value; }
        }

        private string _ChungTuGocKemTheo = null;
        public string ChungTuGocKemTheo
        {
            get { return this._ChungTuGocKemTheo; }
            set { this._ChungTuGocKemTheo = value; }
        }

        private string _GiamDocID = null;
        public string GiamDocID
        {
            get { return this._GiamDocID; }
            set { this._GiamDocID = value; }
        }

        private string _KeToanTruongID = null;
        public string KeToanTruongID
        {
            get { return this._KeToanTruongID; }
            set { this._KeToanTruongID = value; }
        }

        private string _NguoiNopTien = null;
        public string NguoiNopTien
        {
            get { return this._NguoiNopTien; }
            set { this._NguoiNopTien = value; }
        }

        private string _NguoiLapPhieuID = null;
        public string NguoiLapPhieuID
        {
            get { return this._NguoiLapPhieuID; }
            set { this._NguoiLapPhieuID = value; }
        }

        private string _ThuQuyID = null;
        public string ThuQuyID
        {
            get { return this._ThuQuyID; }
            set { this._ThuQuyID = value; }
        }

        private string _DaNhanDuSoTien = null;
        public string DaNhanDuSoTien
        {
            get { return this._DaNhanDuSoTien; }
            set { this._DaNhanDuSoTien = value; }
        }

        private double? _TyGiaNgoaiTe = null;
        public double? TyGiaNgoaiTe
        {
            get { return this._TyGiaNgoaiTe; }
            set { this._TyGiaNgoaiTe = value; }
        }

        private double? _SoTienQuyDoi = null;
        public double? SoTienQuyDoi
        {
            get { return this._SoTienQuyDoi; }
            set { this._SoTienQuyDoi = value; }
        }

        protected override object GetIdValue()
        {
            return _ThuChiID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newThuChiID = -1;
        private RThuChiBO()
        {
            _ThuChiID = newThuChiID--;
        }

        public RThuChiBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public RThuChiBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static RThuChiBO NewRootRThuChiBO()
        {
            return DataPortal.Create<RThuChiBO>(new RootCriteria());
        }

        internal static RThuChiBO NewChildRThuChiBO()
        {
            return DataPortal.Create<RThuChiBO>(new ChildCriteria());
        }

        public static RThuChiBO GetRootRThuChiBO(int _ThuChiID)
        {
            return DataPortal.Fetch<RThuChiBO>(new RootCriteria(_ThuChiID));
        }

        public static RThuChiBO GetRootRThuChiBOByWhere(string where)
        {
            return DataPortal.Fetch<RThuChiBO>(where);
        }

        internal static RThuChiBO GetChildRThuChiBO(SqlDataReader dataReader)
        {
            return new RThuChiBO(dataReader);
        }

        public static void DeleteRThuChiBO(int _ThuChiID)
        {
            DataPortal.Delete(new RootCriteria(_ThuChiID));
        }

        public static void DeleteRThuChiBO(int _ThuChiID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_ThuChiID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int ThuChiID;
            public bool directDelete = false;
            public RootCriteria(int _ThuChiID)
            {
                this.ThuChiID = _ThuChiID;
                this.directDelete = false;
            }

            public RootCriteria(int _ThuChiID, bool directDelete)
            {
                this.ThuChiID = _ThuChiID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int ThuChiID;
            public bool directDelete = false;
            public ChildCriteria(int _ThuChiID)
            {
                this.ThuChiID = _ThuChiID;
                this.directDelete = false;
            }

            public ChildCriteria(int _ThuChiID, bool directDelete)
            {
                this.ThuChiID = _ThuChiID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
        }

        private void This_Fetch(string where)
        {
        }

        private void Fetch(SqlDataReader dataReader)
        {
        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_ThuChiID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_ThuChiID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
